package changgou.pay.controll;

import changgou.pay.service.WeiXinPayService;
import com.alibaba.fastjson.JSON;
import com.github.wxpay.sdk.WXPayUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.util.Map;

@RestController
@RequestMapping(value = "/weixin/pay")
public class WeiXinPayController {

    @Autowired
    private WeiXinPayService weiXinPayService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /***
     * 支付结果通知回调方法
     * @param request
     * @return
     */
    @RequestMapping("/notify/url")
    public String notifyUrl(HttpServletRequest request) throws Exception {
        ServletInputStream inputStream = request.getInputStream();
        //创建一个输出流，输出到文件
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] by = new byte[1024];
        int len;
        while ((len=inputStream.read(by)) != -1){
            byteArrayOutputStream.write(by,0,len);
        }
        //微信支付结果的字节数字
        byte[] bytes = byteArrayOutputStream.toByteArray();
        String xmlResult = new String(bytes,"UTF-8");
        Map<String, String> xmlToMapResult = WXPayUtil.xmlToMap(xmlResult);

        //获取自定义参数
        String attach = xmlToMapResult.get("attach");
        Map<String,String> attachMap = JSON.parseObject(attach, Map.class);

        //发送数据给MQ
        rabbitTemplate.convertAndSend(attachMap.get("exchange"),attachMap.get("routingkey"), JSON.toJSONString(xmlToMapResult));
        String result = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
        return result;
    }

    /***
     *      支付结果查询
     * @param outtradeno  商户生成的订单号
     * @return
     */
    @GetMapping("/status/query")
    public Result quertStatus(String outtradeno){
        Map queryMap = weiXinPayService.queryStatus(outtradeno);
        return new Result(true,StatusCode.OK,"查询成功",queryMap);
    }

    /**
     * 创建二维码 获取支付链接
     * 创建普通二维码
     *  exchange：exchange.order
     *  routingKey:queue.order
     *
     *  秒杀订单：
     *      exchange：exchange.seckillorder
     *      routingKey:queue.seckillorder
     *      exchange+routingkey--》JSON--》attach
     * @param parameterMap
     * @return
     */
    @RequestMapping(value = "create/native")
    public Result createNative(@RequestParam Map<String,String> parameterMap){
        Map<String,String> resultMap = weiXinPayService.createnative(parameterMap);
        return new Result(true, StatusCode.OK,"创建二维码预付订单成功",resultMap);
    }

}
